/**
 * Created by limeng on 16/6/13.
 */
delegateApp.controller("ProposalFeedbackCtrl",
    function ($scope, $rootScope, $http, $routeParams) {
        $scope.proposalId = $routeParams.proposalId;
        $scope.proposal = {};
        $scope.secondMotionList = [];
        $scope.departments = [];
        $scope.secondMotionPageNo = 1;
        $scope.showMoreMotion = false;

        $http.get('/proposal/'
            + $scope.proposalId
        ).success(
            function (data) {
                if (data.state == true) {
                    $scope.proposal = data.proposal;
                    var contentElem = document.getElementById("proposalContent");
                    contentElem.innerHTML = $scope.proposal.content;
                    contentElem.style.padding = 15;

                    $scope.registProposal = {};
                    if ($scope.proposal.hostDepartment) {
                        $scope.registProposal.hostDepartmentId = $scope.proposal.hostDepartment.id;
                    }
                    $scope.registProposal.type = "";

                    $scope.$watch('registProposal.hostDepartmentId', $scope.resetAssistDepartments);
                    $scope.resetAssistDepartments();
                } else {
                    showAlert('查询失败', '原因是:'+data.result);
                }
            }
        ).error(
            function () {
                showAlert('查询失败', '请联系开发者进行维护');
            }
        );

        $scope.refreshMotionList = function() {
            $http.get('/second_motion/' + $scope.proposalId
                + '/10/' + $scope.secondMotionPageNo
            ).success(
                function (data) {
                    if (data.state == true) {
                        for (var i = 0; i < data.list.length; i++) {
                            $scope.secondMotionList.add(data.list[i]);
                        }
                        if ($scope.secondMotionPageNo < data.pageCount) {
                            $scope.showMoreMotion = false;
                        } else {
                            $scope.showMoreMotion = true;
                        }
                    } else {
                        showAlert('查询失败', '原因:'+data.result);
                    }
                }
            ).error(
                function () {
                    showAlert('查询失败', '附议信息数据库出现异常');
                }
            );
        };
        $scope.refreshMotionList();

        $http.get("/department/").success(function (data) {
            if (data) {
                $scope.departments = data;
            } else {
                bootbox.alert("获取部门信息失败!");
            }
        });

        $scope.doFeedback = function () {
            var postData = {};
            postData.id = $scope.proposal.id;
            $http.post("/proposal/feedbackPass", postData).success(function (data) {
                if (data.status == true) {
                    bootbox.alert("操作成功!", function () {
                        history.go(-1);
                    });
                } else {
                    bootbox.alert("创建失败, 原因是" + data.result);
                }
            }).error(function () {
                bootbox.alert("服务器没有响应请求!");
            });
        };
        $scope.proposalReject = function () {
            bootbox.prompt('请输入拒绝执行的理由：', function (result) {
                if (result != null) {
                    $http.post('/proposal/feedbackFailed/' + $scope.proposalId, result)
                        .success(function (data) {
                            if (data.state == true) {
                                bootbox.alert("已退回给提案委！", function () {
                                    history.go(-1);
                                });
                            } else {
                                bootbox.alert("操作失败，原因是" + data.result);
                            }
                        }).error(function () {
                            bootbox.alert("服务器遇到故障！")
                        });
                }
            });
        };

        $scope.resetAssistDepartments = function () {
            $scope.assistDepartments = [];
            var departments = $scope.departments;
            for (var i = 0; i < departments.length; i++) {
                if (departments[i].id != $scope.registProposal.hostDepartmentId) {
                    $scope.assistDepartments.push(departments[i]);
                }
            }
        };
});